//
//  NetworkRequest+Extension.swift
//  Swift_sina
//
//  Created by TFB_WGP on 2017/8/28.
//  Copyright © 2017年 TFB_WGP. All rights reserved.
//

import Foundation
import Alamofire

extension NetworkRequest {

    func statusList(since_id: Int64, max_id: Int64, completion: @escaping (_ list: [[String:AnyObject]]?, _ error:NSError?)->()) {
        
        let urlString = "https://api.weibo.com/2/statuses/public_timeline.json"
        let params = ["since_id":since_id,"max_id":(max_id > 0) ? max_id-1 : 0]
        tokenRequest(urlString: urlString, params: params) { (json, error) in
            
            if let err = error {
                completion(nil,err)
                return
            }
            else{
                let result = json?["statuses"] as? [[String:AnyObject]]
                    completion(result,nil)
            }
        }
    }
    
    /// 返回消息的未读数量
    func unreadCount(completion:@escaping (_ count: Int)->()) {
        
        guard let uid = userAccount.uid else {
            return
        }
        let urlString = "https://rm.api.weibo.com/2/remind/unread_count.json"
        
        let params = ["uid":uid]
        tokenRequest(urlString: urlString, params: params) { (response, error) in
            
            let dict = response as? [String:AnyObject]
            let count = dict?["status"] as? Int
            completion(count ?? 0)
        }
    }
    
}

extension NetworkRequest {
    
    /// 获取accessToken
    func loadAccessToken(code: String, completion: @escaping (_ isSuccess: Bool)->()) -> Void {
        
        let  url = "https://api.weibo.com/oauth2/access_token"
        let  params = ["client_id": WBAppKey,
                       "client_secret": WBAppSecret,
                       "grant_type": "authorization_code",
                       "code":code,
                       "redirect_uri":WBRedirect_uri]
       request(method: .post, urlString: url, params: params) { (json, error) in

        self.userAccount.yy_modelSet(with: json as? [String: AnyObject] ?? [:])
        
        
        self.loadUserInfo(completion: { (dict) in
            
            //设置用户信息
            self.userAccount.yy_modelSet(with: dict)
            //保存模型
            self.userAccount.saveAccount()
            print(self.userAccount)
            
            completion(true)
        })
        
        if error != nil {
            completion(false)
        }else{
            completion(true)
        }
        }
    }
}

//MARK: - 加载用户信息
extension NetworkRequest {
    
    func loadUserInfo(completion: @escaping (_ dict: [String: AnyObject])->()) -> Void {
        
        guard let uid = userAccount.uid else {
            return
        }
        
        let urlString = "https://api.weibo.com/2/users/show.json"
        let  params = ["uid":uid]
        
        tokenRequest(urlString: urlString, params: params) { (json, error) in
            
            completion(json as! [String: AnyObject])
        }
    }
}
